import Cookies from 'js-cookie'

const app = {
  state: {
    sidebar: {
      opened: !+Cookies.get('sidebarStatus')
    },
    visitedViews: [],
    menuMain: [],
    funsList: [],
    AddBtnMain: null,
    modifyBtnMian: null,
    infoBtnMian: null,
    userNameLog: null,
    userAuthorization: null,
    currentBtns: [],
  },
  mutations: {
    TOGGLE_SIDEBAR: state => {
      // if (state.sidebar.opened) {
      //     Cookies.set('sidebarStatus', 1)
      // } else {
      //    Cookies.set('sidebarStatus', 0)
      //}
      console.log(state.sidebar.opened);
      state.sidebar.opened = !state.sidebar.opened
    },
    ADD_VISITED_VIEWS: (state, view) => {
      if (state.visitedViews.some(v => v.path === view.path)) return
      if (state.visitedViews.length === 5) {
        state.visitedViews.splice(0, 1);
      }
      state.visitedViews.push({
        name: view.name,
        path: view.path
      })
      for(const value of state.visitedViews){
        if(value.name == '业务收费'){
          value.name = '结账管理'
        }
        if(value.name == '零售收费'){
          value.name = '零售管理'
        }
      }
      sessionStorage.setItem("visitedViews", JSON.stringify(state.visitedViews));
    },
    DEL_VISITED_VIEWS: (state, view) => {
      let index
      for (const [i, v] of state.visitedViews.entries()) {
        if (v.path === view.path) {
          index = i
          break
        }
      }
      state.visitedViews.splice(index, 1)
      sessionStorage.setItem("visitedViews", JSON.stringify(state.visitedViews));
    }
  },
  actions: {
    ToggleSideBar({
      commit
    }) { //菜单栏折叠
      commit('TOGGLE_SIDEBAR')
    },
    addVisitedViews({
      commit
    }, view) {
      commit('ADD_VISITED_VIEWS', view)
    },
    delVisitedViews({
      commit,
      state
    }, view) {
      return new Promise((resolve) => {
        commit('DEL_VISITED_VIEWS', view)
        resolve([...state.visitedViews])
      })
    }
  }
}

export default app
